package com.bwie.controller;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.bwie.entity.ReqMessage;
import com.bwie.entity.RequestData;
import com.bwie.utils.R;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.bwie.entity.*;

@RestController
@RequestMapping("/deepSeek")
public class DeepSeekController {

    private static final String API_URL = "https://api.deepseek.com/v1/chat/completions";
    private static final String API_KEY = "sk-1257c8348ac04531bc4c4b3630f3837d";

    @PostMapping("/ask/{question}")
    public R ask(@PathVariable("question") String question) {

        // 构建请求的 JSON 数据
        RequestData requestData = new RequestData();
        requestData.setModel("deepseek-chat");
        requestData.setMessages( new ReqMessage[]{
                new ReqMessage("user", question)
        });
        requestData.setStream( false);


        String postResult = HttpUtil.createPost(API_URL)
                //这个请求头.header是自己项目需要加的，可以省略
                .header("Content-Type", "application/json")
                //这两个请求头是项目需要加的，可以省略
                .header("Authorization", "Bearer " + API_KEY)
                //传输参数
                .body(JSON.toJSONString(requestData))
                .execute()
                .body();
        System.out.println("查询结果："+postResult);

        ApiResponse apiResponse = JSON.parseObject(postResult, ApiResponse.class);

        return R.OK(apiResponse);
    }

}
